/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.tb_waybill.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.PageNew;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.IdGen;
import com.thinkgem.jeesite.common.utils.ToolsUtil;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.tb_car_info.entity.TbCarInfo;
import com.thinkgem.jeesite.modules.tb_car_info.service.TbCarInfoService;
import com.thinkgem.jeesite.modules.tb_cost.dao.TbCostDao;
import com.thinkgem.jeesite.modules.tb_cost.entity.TbCost;
import com.thinkgem.jeesite.modules.tb_driver.dao.TbDriverDao;
import com.thinkgem.jeesite.modules.tb_driver.entity.TbDriver;
import com.thinkgem.jeesite.modules.tb_fuel_consumption.dao.TbFuelConsumptionDao;
import com.thinkgem.jeesite.modules.tb_fuel_consumption.entity.TbFuelConsumption;
import com.thinkgem.jeesite.modules.tb_waybill.dao.TbWaybillDao;
import com.thinkgem.jeesite.modules.tb_waybill.dao.TbWaybillDetailDao;
import com.thinkgem.jeesite.modules.tb_waybill.entity.TbWaybill;
import com.thinkgem.jeesite.modules.tb_waybill.entity.TbWaybillDetail;

/**
 * 运单管理Service
 * @author ZhangYangZe
 * @version 2017-05-15
 */
@Service
@Transactional(readOnly = true)
public class TbWaybillService extends CrudService<TbWaybillDao, TbWaybill> {

	@Autowired
	private TbDriverDao tbDriverDao;
	@Autowired
	private TbWaybillDetailDao tbWaybillDetailDao;
	@Autowired
	private TbCostDao costDao;
	@Autowired
	private TbFuelConsumptionDao fuelConsumptionDao;
	@Autowired
	private TbCarInfoService carInfoService;
	
	/**
	 * 以运单id查询货物运输和运单 
	 */
	public TbWaybill get(String id) {
		TbWaybill waybill = super.get(id);
		TbWaybillDetail tbWaybillDetail = new TbWaybillDetail();
		tbWaybillDetail.setWaybillId(waybill.getId());
		List<TbWaybillDetail> l = tbWaybillDetailDao.findAllList(tbWaybillDetail);
		waybill.setNumber(l.size());
		int saze = l.size();
		switch (saze) {
			case 1:
				waybill.setTbWaybillDetailOne(l.get(0));
				break;
			case 2:
				waybill.setTbWaybillDetailOne(l.get(0));
				waybill.setTbWaybillDetailTwo(l.get(1));
				break;
			case 3:
				waybill.setTbWaybillDetailOne(l.get(0));
				waybill.setTbWaybillDetailTwo(l.get(1));
				waybill.setTbWaybillDetailThree(l.get(2));
				break;
		default:
			break;
		}
		return waybill;
	}
	
	public List<TbWaybill> findList(TbWaybill tbWaybill) {
		return super.findList(tbWaybill);
	}
	
	/**
	 * 分页查询运单 
	 */
	public PageNew<TbWaybill> findPage(PageNew<TbWaybill> page, TbWaybill tbWaybill) {
		PageNew<TbWaybill> pageResult = super.findPage(page, tbWaybill);
		List<TbWaybill> list = pageResult.getList();
		if(list.size()>0){
			for(int i=0;i<list.size();i++){
				TbWaybill waybill = list.get(i);
				TbWaybillDetail tbWaybillDetail = new TbWaybillDetail();
				tbWaybillDetail.setWaybillId(waybill.getId());
				List<TbWaybillDetail> l = tbWaybillDetailDao.findAllList(tbWaybillDetail);
				waybill.setNumber(l.size());
				int saze = l.size();
				switch (saze) {
					case 1:
						waybill.setTbWaybillDetailOne(l.get(0));
						break;
					case 2:
						waybill.setTbWaybillDetailOne(l.get(0));
						waybill.setTbWaybillDetailTwo(l.get(1));
						break;
					case 3:
						waybill.setTbWaybillDetailOne(l.get(0));
						waybill.setTbWaybillDetailTwo(l.get(1));
						waybill.setTbWaybillDetailThree(l.get(2));
						break;
				default:
					break;
				}
				
			}
		}
		pageResult.setList(list);
		return pageResult;
	}
	/**
	 * 更改运单状态 
	 * @param TbWaybill
	 */
	@Transactional(readOnly = false)
	public void changeStatu(TbWaybill tbWaybill) {
		dao.changeStatu(tbWaybill);
	}
	
	@Transactional(readOnly = false)
	public void save(TbWaybill tbWaybill) {
		super.save(tbWaybill);
	}
	
	@Transactional(readOnly = false)
	public void delete(TbWaybill tbWaybill) {
		super.delete(tbWaybill);
	}
	
	@Transactional(readOnly = false)
	public void saveNew(TbWaybill tbWaybill){
		String id = tbWaybill.getId();
		if(!ToolsUtil.isTrue(id)){
			String tbWaybill_id = IdGen.uuid();
			tbWaybill.setId(tbWaybill_id);
			TbWaybillDetail tbWaybillDetailOne = tbWaybill.getTbWaybillDetailOne() ;
			tbWaybillDetailOne.setWaybillId(tbWaybill_id);
			tbWaybillDetailOne.setId(IdGen.uuid());
			tbWaybillDetailOne.setNum(1);
			tbWaybillDetailDao.insert(tbWaybillDetailOne);
			int number = tbWaybill.getNumber();
			if(number == 2){
				TbWaybillDetail tbWaybillDetailTwo = tbWaybill.getTbWaybillDetailTwo() ;
				tbWaybillDetailTwo.setId(IdGen.uuid());
				tbWaybillDetailTwo.setNum(2);
				tbWaybillDetailTwo.setWaybillId(tbWaybill_id);
				tbWaybillDetailDao.insert(tbWaybillDetailTwo);
			}else if(number == 3){
				TbWaybillDetail tbWaybillDetailTwo = tbWaybill.getTbWaybillDetailTwo() ;
				TbWaybillDetail tbWaybillDetailThree = tbWaybill.getTbWaybillDetailThree() ;
				
				tbWaybillDetailTwo.setId(IdGen.uuid());
				tbWaybillDetailTwo.setWaybillId(tbWaybill_id);
				tbWaybillDetailTwo.setNum(2);
				tbWaybillDetailDao.insert(tbWaybillDetailTwo);
				
				tbWaybillDetailThree.setId(IdGen.uuid());
				tbWaybillDetailThree.setNum(3);
				tbWaybillDetailThree.setWaybillId(tbWaybill_id);
				tbWaybillDetailDao.insert(tbWaybillDetailThree);
			}
			/**
			 * 设置测量状态为运输中
			 */
			TbCarInfo car = carInfoService.get(tbWaybill.getTbCarInfo().getId());
			car.setCarState(2);
			carInfoService.save(car);
			
			User user = UserUtils.getUser();
			Date date = new Date();
			tbWaybill.setCreateDate(date);
			tbWaybill.setUpdateDate(date);
			tbWaybill.setCreateBy(user);
			tbWaybill.setUpdateBy(user);
			dao.insert(tbWaybill);
		}else{
			TbWaybillDetail tbWaybillDetailOne = tbWaybill.getTbWaybillDetailOne() ;
			tbWaybillDetailDao.update(tbWaybillDetailOne);
			int number = tbWaybill.getNumber();
			TbWaybillDetail tbWaybillDetailTwo = tbWaybill.getTbWaybillDetailTwo() ;
			TbWaybillDetail tbWaybillDetailThree = tbWaybill.getTbWaybillDetailThree() ;
			if(number == 1){
				if(tbWaybillDetailTwo != null)tbWaybillDetailDao.delete(tbWaybillDetailTwo);
				if(tbWaybillDetailThree != null)tbWaybillDetailDao.delete(tbWaybillDetailThree);
			}else if(number == 2){
				tbWaybillDetailDao.insert(tbWaybillDetailTwo);
				if(tbWaybillDetailThree != null)tbWaybillDetailDao.delete(tbWaybillDetailThree);
			}else if(number == 3){
				tbWaybillDetailDao.update(tbWaybillDetailTwo);
				tbWaybillDetailDao.update(tbWaybillDetailThree);
			}
			User user = UserUtils.getUser();
			Date date = new Date();
			tbWaybill.setUpdateDate(date);
			tbWaybill.setUpdateBy(user);
			dao.updateNew(tbWaybill);
		}
		
	}
	
	public void updateNew(TbWaybill tbWaybill){
		dao.updateNew(tbWaybill);
	}
	
	/**
	 * 指派司机
	 * @param tbWaybill
	 */
	@Transactional(readOnly = false)
	public void dispatchDriver(TbWaybill tbWaybill){
		tbWaybill.setState("1");
		User u = UserUtils.getUser();
		dao.updateNew(tbWaybill);
		//TODO 修改司机状态
		TbDriver driver = new TbDriver();
		driver.setUpdateBy(u);
		driver.setUpdateDate(new Date());
		driver.setId(tbWaybill.getDriverId());
		driver.setDriverType("1");
		tbDriverDao.updateNew(driver);
		driver.setId(tbWaybill.getDriverTwoId());
		tbDriverDao.updateNew(driver);
		
		
		//TODO 发送短信
		
		
		//TODO 发送push
			
			
	}
	/**
	 * 运输中审核
	 * @param type 0油瓶消耗1费用
	 * @param fuelConsumption
	 * @param cost
	 */
	@Transactional(readOnly = false)
	public void runningFallOrPass(Integer type,TbFuelConsumption fuelConsumption,TbCost cost){
		try{
			switch (type) {
			case 0:
				fuelConsumptionDao.updateNew(fuelConsumption);
				break;
			case 1:
				costDao.updateNew(cost);
				break;
			default:
				break;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * 运输中等待报账的订单
	 */
	public List<TbWaybill> OnTheWayWaybill(){
		return dao.OnTheWayWaybill();
	}
	/**
	 * 根据订单id修改订单状态 
	 * @param TbWaybill
	 */
	@Transactional(readOnly = false)
	public void updateNew(String id,String State){
		if(id != null && id != ""){
			TbWaybill tbWaybill =new TbWaybill();
			tbWaybill.setId(id);
			tbWaybill.setState(State);
			dao.updateNew(tbWaybill);
		}				
	}
	/**
	 * 判断 TbWaybillDetail否为空
	 * @param TbWaybillDetail
	 * @return TbWaybillDetail
	 */
	public TbWaybillDetail tbWaybillDetailIsempty(TbWaybillDetail tbWaybillDetail){
		if((tbWaybillDetail.getReceiptId()==null || tbWaybillDetail.getReceiptId()=="") &&
		   (tbWaybillDetail.getWaybillId()==null || tbWaybillDetail.getWaybillId()=="") &&
		   (tbWaybillDetail.getSettlement()==null || tbWaybillDetail.getSettlement()==null) &&
		   (tbWaybillDetail.getGoodId()==null || tbWaybillDetail.getGoodId()=="") &&
		   tbWaybillDetail.getWeight()==null && 
		   tbWaybillDetail.getUnitPrice()==null &&
		   tbWaybillDetail.getLength()==null
		   ){
		   return null;
		}
		return tbWaybillDetail;
	}
}